package com.web;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.entity.Project;
import com.entity.User;
import com.service.LoginService;
import com.service.ProjectService;

public class NewProjectServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session=request.getSession();
		User user=(User) session.getAttribute("user");
		session.setAttribute("user", user);
		LoginService service = new LoginService();
		List<User> userList = service.findAll();
		request.setAttribute("userList", userList);
		request.getRequestDispatcher("/WEB-INF/pms/newproject.jsp").forward(request, response);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String projectname = request.getParameter("projectname");
		ProjectService service = new ProjectService();
		List<Project> projectList=service.findProjectName();
		if(projectname==null||"".equals(projectname)){
			request.getRequestDispatcher("/WEB-INF/pms/newproject.jsp").forward(request, response);
			return;
		}
		
		for(Project p:projectList){
			
			if(p.getProjectName().equals(projectname)){
				request.getRequestDispatcher("/WEB-INF/pms/newproject.jsp").forward(request, response);
				//response.sendRedirect("newproject.jspx?code=10001");
				return;
			}
		}
		String projectcontent = request.getParameter("projectcontent");
		String manager = request.getParameter("manager");
		String worker = request.getParameter("workers");
		service.save(projectname,projectcontent,manager,worker);	
		response.sendRedirect("main.jspx");
		
	
	}
	
	
}
